package starkuz.lg.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Map;

@Controller
public class YourDomainController {

    @RequestMapping(value = "/checkDomain.html", method = RequestMethod.GET)
    public String checkDomain(@RequestParam(value = "domainName", required = true) String domainName, Map model) throws IOException {
        String domainText = chechDomain(domainName);
        model.put("domainText", domainText);
        return "test";
    }
    private String chechDomain(String domainName) throws IOException {
        StringBuilder answer = null;
        int c;
        Socket s = new Socket("whois.geektools.com", 43);
        InputStream in = s.getInputStream();
        OutputStream out = s.getOutputStream();
        String str = domainName;
        byte buf[] = str.getBytes();
        out.write(buf);
        while ((c = in.read()) != -1) {
            answer.append((char) c);
        }
        s.close();
        return answer.toString();
    }
}
